<template>
	<view class="container">
		<view class="blue-background"></view>
		<!-- 白色主容器 -->
		<view class="main-content">
			<view class="card">
			<!-- 标题 -->
			<view class="form-item">
				<text class="label">标题：</text>
				<input
					class="input"
					v-model="form.title"
					type="text"
					placeholder="请输入活动标题"
					placeholder-class="ph"
					maxlength="50"
				/>
			</view>

			<!-- 内容 -->
			<view class="form-item">
				<text class="label">内容：</text>
				<textarea
					class="textarea"
					v-model="form.content"
					:maxlength="500"
					placeholder="请输入活动内容"
					placeholder-class="ph"
					auto-height
				/>
			</view>

			<!-- 图片/视频 -->
			<view class="form-item media-item">
				<text class="label">图片/视频：</text>

				<view class="uploader">
					<!-- 预览区：若已选择，展示缩略与删除 -->
					<view
						v-if="media"
						class="preview"
						@click="previewMedia"
					>
						<image
							v-if="media.type === 'image'"
							class="preview-img"
							:src="media.path"
							mode="aspectFill"
						/>
						<view v-else class="preview-video">
							<text class="video-icon">▶</text>
						</view>
						<view class="remove" @click.stop="removeMedia">×</view>
					</view>

					<!-- 占位上传框 -->
					<view
						v-else
						class="upload-box"
						@click="chooseMedia"
					>
						<text class="plus">＋</text>
						<text class="upload-tips">点击选择上传</text>
					</view>
				</view>
			</view>
			</view>
		</view>

		<!-- 底部发布按钮 -->
		<view class="publish-wrap">
			<button class="publish-btn" @click="submit">发布</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					title: '',
					content: ''
				},
				media: null // { path, type: 'image' | 'video' }
			}
		},
		methods: {
			chooseMedia() {
				// 仅单选，图片或视频
				uni.chooseMedia({
					count: 1,
					mediaType: ['image', 'video'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						if (res.tempFiles && res.tempFiles.length) {
							const file = res.tempFiles[0]
							this.media = {
								path: file.tempFilePath || file.tempFile,
								type: file.fileType // 'image' | 'video'
							}
						}
					}
				})
			},
			removeMedia() {
				this.media = null
			},
			previewMedia() {
				if (!this.media) return
				if (this.media.type === 'image') {
					uni.previewImage({
						current: this.media.path,
						urls: [this.media.path]
					})
				} else {
					uni.navigateTo({
						url: `/pages/common/video-preview?src=${encodeURIComponent(this.media.path)}`
					})
				}
			},
			submit() {
				if (!this.form.title.trim()) {
					return uni.showToast({ title: '请输入标题', icon: 'none' })
				}
				if (!this.form.content.trim()) {
					return uni.showToast({ title: '请输入内容', icon: 'none' })
				}
				// 模拟提交
				uni.showLoading({ title: '发布中...' })
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({ title: '发布成功', icon: 'success' })
					// 根据需要清空或返回
					// this.reset()
				}, 800)
			},
			reset() {
				this.form.title = ''
				this.form.content = ''
				this.media = null
			}
		}
	}
</script>

<style scoped>
	.container {
		min-height: 100vh;
		background: #f6f7fb;
		padding-bottom: 140rpx;
		position: relative;
	}

	/* 顶部蓝色背景层 */
	.blue-background {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 300rpx;
		background: linear-gradient(120deg, #3385fd 0%, #3385fd 100%);
	}

	/* 主体内容悬浮区域，左右各3%约94%宽 */
	.main-content {
		position: absolute;
		left: 1%;
		right: 1%;
		top: 1rpx;
		z-index: 2;
	}

	.card {
		margin: 20rpx;
		background: #ffffff;
		border-radius: 16rpx;
		padding: 20rpx 20rpx 10rpx 20rpx;
	}

	.form-item {
		display: flex;
		align-items: flex-start;
		padding: 18rpx 0;
	}

	.label {
		width: 160rpx;
		font-size: 30rpx;
		color: #111;
		line-height: 64rpx;
		margin-right: 20rpx;
		white-space: nowrap; 
	}

	.input {
		flex: 1;
		height: 64rpx;
		background: #ffffff;
		border: 2rpx solid #ececec;
		border-radius: 64rpx;
		padding: 0 28rpx;
		font-size: 28rpx;
	}

	.textarea {
		flex: 1;
		min-height: 280rpx;
		background: #ffffff;
		border: 2rpx solid #ececec;
		border-radius: 16rpx;
		padding: 20rpx 24rpx;
		font-size: 28rpx;
		line-height: 1.6;
	}

	.media-item {
		align-items: flex-start;
	}

	.uploader {
		flex: 1;
	}

	.upload-box,
	.preview {
		width: 440rpx;
		height: 320rpx;
		border-radius: 12rpx;
		border: 2rpx solid #e3e4e8;
		background: #fafbfc;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;
		background: #ffffff;
	}

	.plus {
		font-size: 72rpx;
		color: #c6c9cf;
		line-height: 1;
	}

	.upload-tips {
		margin-top: 12rpx;
		color: #c6c9cf;
		font-size: 26rpx;
	}

	.preview-img {
		width: 100%;
		height: 100%;
	}

	.preview-video {
		width: 100%;
		height: 100%;
		background: #e9eefb;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.video-icon {
		font-size: 72rpx;
		color: #90a4f1;
	}

	.remove {
		position: absolute;
		top: 8rpx;
		right: 8rpx;
		width: 44rpx;
		height: 44rpx;
		border-radius: 50%;
		background: rgba(0,0,0,0.5);
		color: #fff;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.publish-wrap {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 40rpx;
		display: flex;
		justify-content: center;
	}

	.publish-btn {
		width: 80%;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		color: #fff;
		border: none;
		border-radius: 44rpx;
		font-size: 32rpx;
		background: linear-gradient(90deg, #3b86ff 0%, #0567e9 100%);
	}
	.ph {
		color: #c0c4cc;
	}
</style>
